package com.gzhryc.shared_device.oem.h5.mng.controllers.config;

import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.config.dao.db.AgreementDocument;
import com.gzhryc.shared_device.oem.code.config.dao.enums.EAgreementCode;
import com.gzhryc.shared_device.oem.code.config.services.AgreementDocumentService;

import javax.servlet.http.HttpServletRequest;

public class AgreementDocumentController {

    @Route(value = { "/config/agreement/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("codeList", EAgreementCode.values());
        return "/config/agreement/list.html";
    }

    @Route(value = "/config/agreement/list.data")
    public ListResult<AgreementDocument> listData(HttpServletRequest request) {
        ListResult<AgreementDocument> result = new ListResult<>(DtoConstants.NULL_DATA,"暂无数据");
        result.getBody().setData(AgreementDocumentService.self().findAll());
        if (result.getBody().getData() != null && result.getBody().getData().size() > 0) {
            result.setCode(DtoConstants.SUCCESS, null);
        }
        return result;
    }

    @Route(value = { "/config/agreement/add.html" })
    public String add(HttpServletRequest request) {
        request.setAttribute("agreementCodeList", EAgreementCode.values());
        return "/config/agreement/edit.html";
    }

    @Route(value = { "/config/agreement/add.do" })
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增失败");
        AgreementDocument agreementDocument = WebUtils.getParameter(request, AgreementDocument.class);
        try {
            if (AgreementDocumentService.self().add(agreementDocument)) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/config/agreement/update.html" })
    public String update(HttpServletRequest request) {
        Long id = WebUtils.getLong(request,"id");
        AgreementDocument agreementDocument = AgreementDocumentService.self().getById(id);
        WebUtils.setAttribute(request, agreementDocument);
        return "/config/agreement/edit.html";
    }

    @Route(value = { "/config/agreement/update.do" })
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "修改失败");
        AgreementDocument agreementDocument = WebUtils.getParameter(request, AgreementDocument.class);
        try {
            if (AgreementDocumentService.self().updateNotNull(agreementDocument)) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/config/agreement/del.do" })
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败");
        Long id = WebUtils.getLong(request,"id");
        try {
            if(AgreementDocumentService.self().deleteById(id)) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
